#include "playscene.h"

PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
{

}


PlayScene::PlayScene(int index){
    this->levelindex=index;
    //设置菜单栏
    selectbar=new SetMenuBar(this);
    //设置返回按钮
    btn_back=new MyPushButton(":/res/BackButton.png");
    btn_back->setParent(this);
    btn_back->move(this->width()-btn_back->width(),this->height()-btn_back->height());
    //单击按钮返回关卡选择界面
    connect(btn_back,&MyPushButton::clicked,[=](){
        QTimer::singleShot(300,[=](){
            this->hide();
            emit this->choosesceneback();
        });
    });

    //初始化二维数组
    dataConfig config;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            gamearray[i][j]=config.mData[this->levelindex][i][j];
        }
    }

    //绘制胜利图标
    QLabel *winlabel=new QLabel(this);
    QPixmap pix;
    pix.load(":/res/LevelCompletedDialogBg.png");
    winlabel->setGeometry(0,0,pix.width(),pix.height());
    winlabel->setPixmap(pix);
    winlabel->move((this->width()-pix.width())*0.5,-pix.height());


    //创建金币的背景图
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            QLabel *label=new QLabel(this);
            label->setGeometry(0,0,50,50);
            label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
            label->move(57 + i*50,200+j*50);

            QString Img;
            if(gamearray[i][j]==1)
                Img=QString(":/res/Coin0001.png");
            else
                Img=QString(":/res/Coin0008.png");
            MyCoin *mycoin=new MyCoin(Img);
            mycoin->setParent(this);
            mycoin->move(59 + i*50,204+j*50);
            mycoin->posX=i;
            mycoin->posY=j;
            mycoin->flag=gamearray[i][j];

            btn_coin[i][j]=mycoin;
            connect(mycoin,&MyCoin::clicked,[=](){//实现金币周围的银币翻转
                mycoin->changeFlag();
                gamearray[mycoin->posX][mycoin->posY]==1?0:1;
                //qDebug()<<"点击的位置：X="<<mycoin->posX<<"点击的位置：Y="<<mycoin->posY;
                if(mycoin->posX+1<=3){//右侧还有金币，则翻转
                    btn_coin[mycoin->posX+1][mycoin->posY]->changeFlag();
                    gamearray[mycoin->posX+1][mycoin->posY]==1?0:1;
                }
                if(mycoin->posX-1>=0){//左侧还有金币，则翻转
                    btn_coin[mycoin->posX-1][mycoin->posY]->changeFlag();
                    gamearray[mycoin->posX-1][mycoin->posY]==1?0:1;
                }
                if(mycoin->posY-1>=0){//上侧还有金币，则翻转
                    btn_coin[mycoin->posX][mycoin->posY-1]->changeFlag();
                    gamearray[mycoin->posX][mycoin->posY-1]==1?0:1;
                }
                if(mycoin->posY+1<=3){//右侧还有金币，则翻转
                    btn_coin[mycoin->posX][mycoin->posY+1]->changeFlag();
                    gamearray[mycoin->posX][mycoin->posY+1]==1?0:1;
                }

                this->IsWin=true;
                for(int i=0;i<4;i++)
                    for(int j=0;j<4;j++){
                        if(btn_coin[i][j]->flag==false){
                            this->IsWin=false;
                            return;
                        }
                    }
                if(this->IsWin){
                    qDebug()<<"游戏胜利!";
                    //将所有按钮的胜利标志都设为true
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                            btn_coin[i][j]->IsWin=true;
                            }
                    }

                    QPropertyAnimation *animation=new QPropertyAnimation(winlabel,"geometry");
                    animation->setDuration(1000);
                    animation->setStartValue(QRect(winlabel->x(),winlabel->y(),winlabel->width(),winlabel->height()));
                    animation->setEndValue(QRect(winlabel->x(),winlabel->y()+114,winlabel->width(),winlabel->height()));
                    animation->setEasingCurve(QEasingCurve::OutBounce);
                    animation->start();
                }


            });
        }
    }





 //设置当前关卡标题
    QLabel *label=new QLabel(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str=QString("Level:%1").arg(this->levelindex);
    label->setText(str);
    label->setGeometry(QRect(30,this->height()-50,120,50));
}

void PlayScene::paintEvent(QPaintEvent *event){

    //设置背景
    QString str=QString(":/res/PlayLevelSceneBg.png");
    selectbackground =new SetSceneBackGround(this,str);
    //创建主界面标题
    QString str_1=QString(":/res/Title.png");
    selecttitle=new SetMainTitle(this,str_1);
}
